<template>
  <div class="table">
    <div class="head">
      <h1>简书</h1>
      <div>
      <img @click="toPersonal" :src="store.state.msg.avatar" alt=""><span>{{store.state.msg.username}}</span></div>
    </div>
    <el-table
      :data="compData.data"
      border
      style="width: 100%"
      @row-click="rowClick($event)"
    >
      <el-table-column fixed prop="title" label="文章标题" width="300">
      </el-table-column>
      <el-table-column prop="time" label="发布时间" width="300">
      </el-table-column>
      <el-table-column prop="author" label="文章作者" width="300">
      </el-table-column>
      <el-table-column prop="read" label="阅读量" width="300">
      </el-table-column>
    </el-table>
    <el-pagination
      background
      @current-change="getCurrentPage(event)"
      layout="prev, pager, next"
      :total="totalNums.total"
    >
    </el-pagination>
  </div>
</template>

<script setup>
import { onMounted, reactive } from "@vue/runtime-core";
import { http } from "../../utils/http";
import { formatTime } from "../../utils/commonUtils/formatTime";
import { useRouter, useRoute } from "vue-router";
import { useStore } from 'vuex'
const store = useStore()
const route = useRoute();
const router = useRouter();
const params = reactive({
  page: 1,
});
let totalNums = reactive({ total: 0 });
const compData = reactive({ data: [] });
onMounted(async () => {
  let res = await http.post(`/article/find?page=${params.page}`);
  for (let i = 0; i < res.data.length; i++) {
    const element = res.data[i];
    element["time"] = formatTime(element.createTime);
  }
  compData.data = res.data;
  totalNums.total = res.total;
});
const getCurrentPage = async (page) => {
  params.page = page;
  const result = await http.get(`/article/find?page=${params.page}`);
  compData.data = result.data;
};

// 点击某一行到时候
const rowClick = (event) => {
  router.push({
    name: "Detail",
    query: { id: event.id },
  });
};

// 个人中心
const toPersonal = () => {
  router.push({name:"AdminArticle"})
}
</script>

<style lang="scss" scoped>
.table {
  width: 80%;
  margin: 0 auto ;
  .el-table {
    height: 600px;
  }
  .head{
    height: 100px;
    background: #eee;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 20px;
    h1{
      cursor: pointer;
    }
    img{
      height: 50px;
      width: 50px;
      border-radius: 50%;
      cursor: pointer;
      margin: 0 20px;
    }
    div{
      display: flex;
      align-items: center;
    }
  }
}
</style>